<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class User extends CI_Controller {
    
    function __construct() {
        parent::__construct();
    }
    
    function check(){
        $id = $this->session->userdata('user_id');
        if(empty($id)){
            redirect('');
        }
    }
    
    /**** LOGIN ****/

    function index() {
        $user_id = $this->session->userdata('user_id');
        $header=array();
        $data=array();
        if(empty($user_id)){
            //show login-register
            $header['css'] = array('user');
            $this->load->view('header',$header);
            $this->load->view('user/login');
            $this->load->view('footer');
        }else{
            //show home
            $header['css'] = array('user');
            $data['user'] = $this->m_user->get_user($user_id);
            $this->load->view('header',$header);
            $this->load->view('user/home', $data);
            $this->load->view('footer');
        }
    }

    function process_login() {
        $header['css'] = array('user');
        $email = $this->input->post('email');
        $pass = $this->input->post('password');
        //Ambil record
        $user = $this->m_user->get_userid($email);
        //Bandingkan
        if ($user->num_rows() > 0) {
            $user = $user->first_row();
            $salt = $user->salt;
            $password = $user->password;
            $hash = md5($pass.$salt);
            if ($password == $hash) {
                //Set Session
                $this->session->set_userdata('user_id',$user->id);

                //go to profile
                redirect();
            } else {
                $this->load->view('header',$header);
                $this->load->view('user/login');
                $this->load->view('footer');
            }
        } else {
            $this->load->view('header',$header);
            $this->load->view('user/login');
            $this->load->view('footer');
        }
    }
    
    function process_register() {
        //load model

        //Get data from form
        $email = $this->input->post('email');
        $pass = $this->input->post('password');
        $confPass = $this->input->post('conf_password');
        $name = $this->input->post('name');
        //$date = $this->input->post('date');
        $date = date("Y-m-d"); //ngasal

        //Check email
        $query = $this->m_user->get_userid($email);

        if ($query->num_rows() > 0) {
            $data = array('status_register' => 'Email have been used',
                'Default' => ""
            );
            $this->load->view("user/register", $data);
        } else {
            //Create Salt
            $salt = '';

            //insert DB
            $dataUser = array(
                'email' => $email,
                'password' => $pass,
                'salt' => $salt,
                'nama' => $name,
                'tanggal_lahir' => $date,
                'is_aktif' => 0
            );
            $id = $this->m_user->insert_user($email,$pass,$name,$date);
            $this->session->set_userdata('user_id',$id);
            redirect('user/info');
        }
    }
    
    /**** LOGOUT ****/

    function logout() {
        $this->session->sess_destroy();
        redirect('');
    }
    
    /***** INFO *****/
    function info(){
        $this->check();
        $data['user'] = $this->m_user->get_user($this->session->userdata('user_id'));
        $header['css'] = array('user');
        $this->load->view('header',$header);
        $this->load->view('user/info');
        $this->load->view('footer');
    }

}